package com.feng.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.feng.entity.User;
import com.feng.entity.UserConfig;
import com.feng.service.IUserConfigService;
import com.feng.service.IUserService;
import com.feng.util.CommonUtils;
import com.feng.util.CookieUtils;
import com.feng.util.Dict;
import com.feng.util.RedisUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 用户
 * @author qf
 *
 */
@Controller
public class UserController {

	private final Log log= LogFactory.getLog(UserController.class);
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IUserConfigService userConfigService;
	@Autowired
	private RedisUtils redisUtils;
	
	//session超时时间
	@Value("${userSession.timout}")
	private long timout;
	//redis开关
	@Value("${mblog.redis.enabled}")
	private Boolean redisIsEnabled;
	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @param uname
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping("userLogin")
	public String userLogin(HttpServletRequest request,HttpServletResponse response,String uname,String password){
		User user = null;
		try{
			user = userService.findUser(uname,password);
		}catch(Exception e){
			log.error("查询用户信息失败！"+e.toString());
		}
		if(user != null){
			//登录成功
			if(redisIsEnabled) {
				String uuid = CommonUtils.getUUID();
				CookieUtils.saveCookie(response, Dict.USER_LOGIN_COOKIE,uuid);
				redisUtils.setString(uuid, JSON.toJSONString(user), timout);//cookieid 存入redis
				
				//把最后一次登录的cookid与用户绑定
				redisUtils.setString(user.getId(), uuid,timout);
			}
			//将用户id存入session
			request.getSession().setAttribute(Dict.SESSION_USERID, user.getId());
			request.getSession().setAttribute(Dict.SESSION_USERNAME, user.getName());
			return "1";
		}
		return "0";
	}
	
	/**
	 * 检查用户是否已经登录
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("ckUserIsLogin")
	public String ckUserIsLogin(HttpServletRequest request){
		
		String userCookie = CookieUtils.getUserCookie(request,Dict.USER_LOGIN_COOKIE);
		
		if(CommonUtils.isEmpty(userCookie)) {//未登录
			return "0";
		}else {
			String userInfo = redisUtils.getString(userCookie);
			if(CommonUtils.isNotEmpty(userInfo)){
				//与redis中的cookieid进行比较
				User user = JSON.parseObject(userInfo, User.class);
				String cookieId = redisUtils.getString(user.getId());//获取最新的cookieid
				return "1";//已登录
			}
		}
		return "0";
	}
	
	/**
	 * 检查用户会话是否失效
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("ckUserInvalid")
	public String ckUserInvalid(HttpServletRequest request){
		boolean flag = true;
		try {
			if(redisIsEnabled) {
				flag = userService.ckUserInvalid(request);
				if(!flag) {
					return "1";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "0";
	}
	
	@ResponseBody
	@RequestMapping("setUserSync")
	public String setUserSync(HttpServletRequest request,String sync){
		try {
			userService.setUserSync(request, sync);
			return "1";
		} catch (Exception e) {
			log.error("userconfigInfo.update error......",e);
			return "0";
		}
		
	}
	@RequestMapping("/")
	public String toLogin() {
		return "login";
	}
	
	
}
